function include(page, data) {
    if (data == null) {
        data = {random: Date.parse(new Date())}
    } else {
        data.random = Date.parse(new Date());
    }
    $.ajax({
        url: page,
        type: "GET",
        async: true,
        dataType: "html",
        data: data,
        success: function (res) {
            $('body').append(res)
        },
        error: function (err) {
        }
    });
}

function showPushButton() {
    $(".push-box").animate({
        right: -15
    }, function () {
        setTimeout(function () {
            $(".push-box").animate({
                right: -100
            })
        }, 3000)
    });
}

function loadPageLayout(page, layout, data) {
    //clearthatPageInterval();
    layout.css({
        textAlign: 'center',
        lineHeight: layout.height() + "px",
    });
    if (data == null) {
        data = {}
    }
    data.t = (new Date()).getTime();
    layout.html("<img src='./images/progressBar_m.gif'>");
    $.ajax({
        url: page,
        type: "GET",
        async: true,
        dataType: "html",
        data: data,
        success: function (res) {
            layout.css({
                textAlign: 'left',
                lineHeight: '1.3'
            });
            layout.html(res);
        },
        error: function (err) {
            layout.html("模块加载失败");
            layout.fadeIn();
        }
    });
}

function backToHome() {
    //other-ops-layout
    $("#home-ops-layout").hide();
    $("#popLayout").fadeOut();
    onBack();
    $("#popLayoutContent").empty();
    $('body').css({
        overflowY: 'auto'
    });
}

function onBack() {

}

function showPushForm() {
    event.stopPropagation();
    $(".push-box").animate({
        right: -100
    });
    $("#home-ops-layout").show();
    $("#popLayout").fadeIn();
    loadPageLayout('./portlet/post-form.html', $("#popLayoutContent"));
    $('body').css({
        overflowY: 'hidden'
    });
}

function loadStyleSheet(css) {
    $("<link>").attr({
        rel: "stylesheet",
        type: "text/css",
        href: css + "?t=" + (new Date()).getTime()
    }).appendTo("head");
}

function buildPagepart(layoutId, total, currentPage, pageSize, totalPage, goPageFunc) {
    var buf = new StringBuffer();
    buf.append('<div style="float:left;margin-left:10px;" id="' + layoutId + '-record" class="pagepart">');
    buf.append('共 <span id="' + layoutId + '-total">' + total + '</span> 条记录，');
    buf.append('当前显示 <span id="' + layoutId + '-pagesize">' + pageSize + '</span> 条记录，');
    buf.append('<span id="' + layoutId + '-pageno">' + currentPage + '</span>/<span id="' + layoutId + '-totalpage">' + totalPage + '</span> 页数据');
    buf.append('</div>');
    buf.append('<div style="float:right;margin-right:30px;" id="' + layoutId + '-page" class="pagepart-go">');
    if (currentPage > 1) {
        buf.append('        <click id="' + layoutId + '-first-page">首页</click>');
        buf.append('            <click id="' + layoutId + '-pre-page">上一页</click>');
    }
    if (currentPage < totalPage) {
        buf.append('        <click id="' + layoutId + '-next-page">下一页</click>');
        buf.append('        <click id="' + layoutId + '-last-page">最后</click>');
    }
    buf.append('    </div>');
    $("#" + layoutId).html(buf.toString());
    $("#" + layoutId + '-page click').click(function () {
        var cmd = $(this).text();
        if (cmd == "首页") {
            goPageFunc(1);
        } else if (cmd == "上一页") {
            goPageFunc(currentPage - 1);
        } else if (cmd == "上一页") {
            goPageFunc(currentPage - 1);
        } else if (cmd == "下一页") {
            goPageFunc(currentPage + 1);
        } else if (cmd == "最后") {
            goPageFunc(totalPage);
        }
    });
}

function onScroll() {
}

function closeTodayHistory() {
    event.stopPropagation();
    $(".history-topic").hide();
    $(".history-today-box icon").hide();
    $(".history-today-content").hide();
    $(".history-today-box").css({height: 60});
    $(".history-today-box").animate({
        width: 70
    }, function () {
        $(".history-today-box").removeClass("history-today-show-box");
    });
}

var historyEvent = {};
var currentDate = null;

function showTodayHistory() {
    currentDate = new Date();
    $(".history-today-box").animate({
        width: 350,
    }, function () {
        $(".history-today-box").addClass("history-today-show-box");
        $(".history-topic").fadeIn();
        $(".history-today-box icon").fadeIn();
        $(".history-today-content").fadeIn();
        $(".history-today-box").css({
            height: 50 + $(".history-today-content").outerHeight(true)
        });
        var today = currentDate.format("MM-dd");
        var todayKey = currentDate.format("MMdd");
        loadHistoryEvent(today, todayKey);
    });
}

function preDayHistoryEvent() {
    event.stopPropagation();
    currentDate.setDate(currentDate.getDate() - 1);
    var today = currentDate.format("MM-dd");
    var todayKey = currentDate.format("MMdd");
    loadHistoryEvent(today, todayKey);
}
function nextDayHistoryEvent() {
    event.stopPropagation();
    currentDate.setDate(currentDate.getDate() + 1);
    var today = currentDate.format("MM-dd");
    var todayKey = currentDate.format("MMdd");
    loadHistoryEvent(today, todayKey);
}

function loadHistoryEvent(day, dayKey) {
    if (!(dayKey in historyEvent)) {
        $(".history-today-content").html('<div style="padding:10px;">正在加载历史上的今天...</div>');
        $.ajax({
            url: '/topic/getHistoryEvent',
            type: "GET",
            async: true,
            dataType: "json",
            data: {today: day},
            success: function (res) {
                if (res.errorCode == 0) {
                    var ds = day.split("-");
                    historyEvent = res.item[ds[0]];
                    buildHistoryEvent(res.key, res.today);
                }
            },
            error: function (err) {

            }
        });
    } else {
        buildHistoryEvent(dayKey, day);
    }
}

function buildHistoryEvent(dayKey, day) {
    var items = historyEvent[dayKey];
    var buf = new StringBuffer();
    var all = true;
    buf.append('<div class="history-day-layout">');
    buf.append(day);
    buf.append('<icon class="icon-zuokuohao" onclick="preDayHistoryEvent()"></icon>');
    buf.append('<icon class="icon-youkuohao" onclick="nextDayHistoryEvent()"></icon>');
    buf.append('</div>');
    buf.append('<table border="0" width="90%" cellspacing="0" cellpadding="0" style="padding:10px;">');
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (all || item.recommend) {
            buf.append('<tr>');
            buf.append('<td valign="top" style="font-size:18px;width:60px;color:#ff0000;">' + item.year + ' <span style="font-size:12px;color:#666">年</span></td>');
            buf.append('<td valign="top" align="center" style="width:20px;"><div class="event-circle"></div>');
            if (i + 1 < items.length)
                buf.append('<div class="event-line"></div>');
            buf.append('</td>');
            buf.append('<td valign="top" style="font-size:16px;color:#666;" class="event-content">' + item.title);
            if (item.pic_index) {
                buf.append('<div><img src="' + item.pic_index + '" onload="resetHistoryLayoutHeight()"></div>');
            }
            if (item.cover) {
                var desc = item.desc;
                if (desc.length > 28)
                    desc = desc.substring(0, 27) + '...';
                buf.append(desc);
            }
            buf.append('</td>');
            buf.append('</tr>');
        }
    }
    buf.append('</table>');
    if (all)
        buf.append('<div style="height:50px;"></div>');
    $(".history-today-content").html(buf.toString());
    resetHistoryLayoutHeight();
}

function resetHistoryLayoutHeight() {
    var trs = $(".history-today-content table tr");
    for (var i = 0; i < trs.length; i++) {
        var tr = $(trs[i]);
        var h = tr.outerHeight(true) - tr.find(".event-circle").outerHeight(true) - 10;
        tr.find(".event-line").css({
            height: h
        })
    }
    $(".history-today-content").css({
        maxHeight: document.body.clientHeight - 200
    });
    $(".history-today-box").css({
        height: 50 + $(".history-today-content").outerHeight(true),
        maxHeight: document.body.clientHeight - 200
    });
    $(".history-today-content").getNiceScroll().resize();
}